package com.meteor.downloader.videodownloader.activity

import android.content.Intent
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.lifecycleScope
import com.airbnb.lottie.LottieAnimationView
import com.meteor.downloader.videodownloader.R
import com.meteor.downloader.videodownloader.activity.downloader.MdmDownloadingActivity
import com.meteor.downloader.videodownloader.activity.downloader.MdmInputLinkActivity
import com.meteor.downloader.videodownloader.activity.downloader.MdmVideoManagerActivity
import com.meteor.downloader.videodownloader.activity.set.MdmHelpYouActivity
import com.meteor.downloader.videodownloader.activity.set.MdmSettingActivity
import com.meteor.downloader.videodownloader.admdm.MdmDealAdUtils
import com.meteor.downloader.videodownloader.databinding.ActivityMainMdmBinding
import com.meteor.downloader.videodownloader.dismissGoMdm
import com.meteor.downloader.videodownloader.mdmGetPxDp
import com.meteor.downloader.videodownloader.ui.MdmDownloadTipView
import com.meteor.downloader.videodownloader.utils.ApplicationMdmUtils
import com.meteor.downloader.videodownloader.utils.ApplicationMdmUtils.mVideoMdmList
import com.meteor.downloader.videodownloader.utils.DataManagerMdmUtils
import com.meteor.downloader.videodownloader.utils.MdmTotalUtils
import com.meteor.downloader.videodownloader.utils.dialog.DialogInitMdmLoading
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_DOWNLOAD_TIP
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_ENTER_MAIN_RESULT
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainMdmActivity : MdmBindingActivity<ActivityMainMdmBinding>() {
    private var mMdmNotifyGrandTotal = 0
    private var mMdmNotifyType = ""

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        intent?.let {
            mMdmNotifyType = it.getStringExtra("mdmNotifyType") ?: ""
            mMdmNotifyGrandTotal = it.getIntExtra("mdmNotifyGrandTotal", 0)
            if (mMdmNotifyType == "true") {
                try {
                    if (mVideoMdmList.isEmpty()) {
                        ApplicationMdmUtils.getMeteorVideoSome()
                    }
                    val data = mVideoMdmList[mMdmNotifyGrandTotal]
                    mLinkSearch = data.urlMdm ?: ""
                } catch (_: Exception) {
                }
                mMdmBinding.flStartGo.performClick()
            }
        }
    }

    private var mLinkSearch = ""
    private var mFirst = false

    override fun beginViewAndDoMdm() {
        mStarNativeValue = true
        mNativeBookLayout = mMdmBinding.flMdmNativeView
        DataManagerMdmUtils.setDataKeyMdm(MDM_ENTER_MAIN_RESULT, true)
        mMdmBinding.ivSetting.setOnClickListener {
            showBookAdMdm("mdmEnt", false) {
                startActivity(Intent(this, MdmSettingActivity::class.java))
            }
        }
        mMdmBinding.ivDownloading.setOnClickListener {
            showBookAdMdm("mdmEnt", false) {
                startActivity(Intent(this, MdmDownloadingActivity::class.java))
            }
        }
        mMdmBinding.ivVideo.setOnClickListener {
            showBookAdMdm("mdmEnt", false) {
                startActivity(Intent(this, MdmVideoManagerActivity::class.java))
            }
        }
        mMdmBinding.ivHelp.setOnClickListener {
            showBookAdMdm("mdmEnt", false) {
                startActivity(Intent(this, MdmHelpYouActivity::class.java))
            }
        }
        mMdmBinding.flStartGo.setOnClickListener {
            startActivity(Intent(this, MdmInputLinkActivity::class.java).apply {
                if (mLinkSearch.isNotEmpty()) {
                    putExtra("linkSearch", mLinkSearch)
                }
                if (mFirst) {
                    mFirst = false
                    putExtra("isFirst", true)
                }
            })
            mLinkSearch = ""
        }
        intent?.let {
            mMdmNotifyType = it.getStringExtra("mdmNotifyType") ?: ""
            mMdmNotifyGrandTotal = it.getIntExtra("mdmNotifyGrandTotal", 0)
            if (mMdmNotifyType == "true") {
                try {
                    if (mVideoMdmList.isEmpty()) {
                        ApplicationMdmUtils.getMeteorVideoSome()
                    }
                    val data = mVideoMdmList[mMdmNotifyGrandTotal]
                    mLinkSearch = data.urlMdm ?: ""
                } catch (_: Exception) {
                }
                mMdmBinding.flStartGo.performClick()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch(Dispatchers.Main) {
            delay(50)
            launch(Dispatchers.Main) {
                addPathViewMdm()
            }
            lifecycleScope.launch(Dispatchers.Main) {
                delay(288)
                MdmDealAdUtils.loadBookIntersMdm()
            }
        }
    }

    private fun addPathViewMdm() {
        if (!MdmTotalUtils.getMdmAppStatus()) return
        mMdmBinding.flStartGo.post {
            if (!DataManagerMdmUtils.getDataKeyMdm(MDM_DOWNLOAD_TIP, false)) {
                val pathLocationView = MdmDownloadTipView(this, mMdmBinding.flStartGo)
                val location = IntArray(2)
                mMdmBinding.flStartGo.getLocationOnScreen(location)
                val noticeView = LottieAnimationView(this).apply {
                    setAnimation(R.raw.mdm_notice_let)
                    loop(true)
                    playAnimation()
                }
                noticeView.layoutParams = ConstraintLayout.LayoutParams(
                    200.mdmGetPxDp(), 200.mdmGetPxDp()
                ).apply {
                    startToStart = ConstraintLayout.LayoutParams.PARENT_ID
                    topToTop = ConstraintLayout.LayoutParams.PARENT_ID
                    topMargin =
                        (location[1] + mMdmBinding.flStartGo.height - 100f.mdmGetPxDp()).toInt()
                }
                pathLocationView.addView(noticeView)
                mMdmBinding.root.addView(pathLocationView)
                pathLocationView.setOnClickListener {
                    DataManagerMdmUtils.setDataKeyMdm(MDM_DOWNLOAD_TIP, true)
                    mMdmBinding.root.removeView(pathLocationView)
                    mFirst = true
                    mMdmBinding.flStartGo.performClick()
                }
            }
        }
    }

    private fun showBookAdMdm(
        type: String, showTip: Boolean = false, statusLook: Boolean = true, mdmBack: () -> Unit
    ) {
        if (statusLook && !MdmTotalUtils.getMdmAppStatus()) {
            mdmBack()
            return
        }
        if (showTip) {
            if (MdmDealAdUtils.getVlaueForInters() != null) {
                showRootBookAdBack(type, statusLook, mdmBack)
                return
            }
            val dialog = DialogInitMdmLoading(this)
            dialog.show()
            MdmDealAdUtils.loadBookIntersMdm(true, statusLook)
            lifecycleScope.launch(Dispatchers.Main) {
                delay(1600)
                if (MdmDealAdUtils.getVlaueForInters() != null) {
                    showRootBookAdBack(type, statusLook, mdmBack)
                    delay(96)
                    dialog.dismissGoMdm()
                    return@launch
                }
                if (MdmDealAdUtils.getVlaueForInters() != null) {
                    delay(1600)
                    showRootBookAdBack(type, statusLook, mdmBack)
                    delay(100)
                    dialog.dismissGoMdm()
                    return@launch
                }
                delay(1600)
                showRootBookAdBack(type, statusLook, mdmBack)
                delay(96)
                dialog.dismissGoMdm()
            }
        } else {
            showRootBookAdBack(type, statusLook, mdmBack)
        }
    }

    override fun getLayoutValue(): ActivityMainMdmBinding {
        return ActivityMainMdmBinding.inflate(layoutInflater)
    }
}